package com.tql.test_11;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/*
* 异步回调  类似AJAX
* */
public class AyscTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
       /* //异步回调无返回值
        CompletableFuture<Void> completableFuture=CompletableFuture.runAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("helllo");
        });
        completableFuture.get();
        System.out.println("11111");*/
        //支持异步回调
        CompletableFuture<Integer> completableFuture=CompletableFuture.supplyAsync(()->{
            System.out.println("hello");
            int i=10/0;
            return 200;
        });
        completableFuture.whenComplete((t,u)->{
            System.out.println(t);
            System.out.println(u);
        }).exceptionally((e)->{
            System.out.println(e);
            return 404;
        });
    }
}
